package MyQueue;

public class MyQueue {
	static class ListNode {
		private int val;
		private ListNode prev;
		private ListNode next;

		public ListNode(int val) {
			this.val = val;
		}
	}
	private ListNode front;
	private ListNode rear;
	private int usedSize;
	public void display() {
		ListNode cur=front;
		while(cur!=null) {
			System.out.print(cur.val+ " ");
			cur=cur.next;
		}
		System.out.println();
	}
	//相当于尾插
	public void offer(int val) {
		ListNode node=new ListNode(val);
		if(isEmpty()) {
			front=rear=node;
			this.usedSize++;
			return ;
		} else {
			rear.next=node;
			node.prev=rear;
			rear=node;
			usedSize++;
		}
	}
	//头删除
	public int poll() {
		if(isEmpty()) {
			throw new pollException("队列为空无法删除");
		}
		int len=front.val;

		//不为空的情况下，有两种情况，一个是一个节点，另一个节点的数量大于1
		if(front==rear) {
			front=rear=null;
			this.usedSize--;
			return len;
		}
		front=front.next;
		front.prev=null;
		this.usedSize--;
		return len;
	}
	public int peek() {
		if(!isEmpty()) {
			return front.val;
		}
		return -1;
	}
	public int getUsedSize() {
		return this.usedSize;
	}
	public boolean isEmpty() {
		return this.usedSize==0;
	}
}
